package classes;

public class LifebookUserSearchParameters {

    public static final int SEARCH_FOLLOWERS = 0;
    public static final int SEARCH_FOLLOWING = 1;
    public static final int SEARCH_NOT_FOLLOWING = 2;
    public static final int SEARCH_ALL = 3;
    private String username;
    private String currentUserUsername;
    private int searchMode;

    public LifebookUserSearchParameters(String username, String currentUserUsername, int searchMode) {
        this.username = username;
        this.currentUserUsername = currentUserUsername;
        this.searchMode = searchMode;
    }

        public LifebookUserSearchParameters(String currentUsername, int searchMode) {
        this("", currentUsername, searchMode);
    }
    
    public LifebookUserSearchParameters(String username) {
        this(username, "", 3);
    }

    public LifebookUserSearchParameters() {
        this("", "", 3);
    }

    public String getSearchString() {
        switch (searchMode) {
            case SEARCH_FOLLOWERS:
                return getSearchFollowers();
            case SEARCH_FOLLOWING:
                return getSearchFollowing();
            case SEARCH_NOT_FOLLOWING:
                return getSearchNotFollowing();
            default:
                return getSearchAll();
        }
    }

    private String getSearchAll() {
        String searchTerm = "select users.username, email, password, firstName, lastName, sex, birthDay, aboutMe, \n"
                + "profileImage, isEnabled, follower,count(followers.follower) as followerCount from users \n"
                + "left join followers on followers.username = users.username\n"
                + "where isEnabled = 1 \n"
                + "and users.username like '%" + username + "%'\n"
                + "group by users.username\n"
                + "order by followerCount desc";
        return searchTerm;
    }

    private String getSearchFollowers() {
        String searchTerm = "select users.username, email, password, firstName, lastName, sex, birthDay, aboutMe, \n"
                + "profileImage, isEnabled, follower,count(followers.follower) as followerCount from users \n"
                + "left join followers on followers.username = users.username\n"
                + "where isEnabled = 1 and users.username in (select follower from followers\n"
                + "where username = '" + currentUserUsername + "')\n"
                + "and users.username like '%" + username + "%'\n"
                + "group by users.username\n"
                + "order by followerCount desc";
        return searchTerm;
    }

    private String getSearchFollowing() {
        String searchTerm = "select users.username, email, password, firstName, lastName, sex, birthDay, aboutMe, \n"
                + "profileImage, isEnabled, follower,count(followers.follower) as followerCount from users \n"
                + "left join followers on followers.username = users.username\n"
                + "where isEnabled = 1 and users.username in (select username from followers\n"
                + "where follower = '" + currentUserUsername + "')\n"
                + "and users.username like '%" + username + "%'\n"
                + "group by users.username\n"
                + "order by followerCount desc";
        return searchTerm;
    }

    private String getSearchNotFollowing() {
        String searchTerm = "select users.username, email, password, firstName, lastName, sex, birthDay, aboutMe, \n"
                + "profileImage, isEnabled, follower,count(followers.follower) as followerCount from users \n"
                + "left join followers on followers.username = users.username\n"
                + "where isEnabled = 1 and users.username not in (select username from followers\n"
                + "where follower = '" + currentUserUsername + "')\n"
                + "and users.username like '%" + username + "%'\n"
                + "group by users.username\n"
                + "order by followerCount desc";
        return searchTerm;
    }
}
